<template>
  <!-- 绑定查询-->
  <m-win
    :wd="1200"
    v-model:window_open="data.win_open_bind_sea"
    :title="`查询（${data.ckdata.guild_name}）公会绑定群信息`"
  >
    <component :is="set.comp.bind_sea" :guild="data.ckdata" />
  </m-win>

  <!-- 编辑按钮-->
  <m-win
    :is="cfg.model"
    v-model:window_open="data.win_open"
    :title="`编辑${data.ckdata.guild_name}公会数据`"
  >
    <div class="_box0">
      <div>
        <n-space>
          <n-button size="small" @click="set.del_guild_all"> 删除全数据 </n-button>
          <n-button size="small" @click="set.del_season_cachefile">
            删除赛季缓存文件
          </n-button>
          <n-button size="small" @click="set.del_allbind"> 清空绑定群 </n-button>
        </n-space>
      </div>
      <n-hr />
      <div>
        <n-space>
          <n-button size="small" @click="set.open_bind_sea"> 绑定群查询 </n-button>
        </n-space>
      </div>
    </div>
  </m-win>

  <div class="boxgap">
    <m-page :page="data.page" :count="data.count" @ck="set.page_up_page" />
    <div style="margin-top: 4px">
      <div class="noa" v-if="$set.mb == false">
        <div class="_list" style="min-width: 850px">
          <div class="_listbox">
            <div style="width: 200px" class="_df">公会名称</div>
            <div style="width: 125px" class="_df">会战日期</div>
            <div style="width: 125px" class="_df">最后同步时间</div>
            <div style="width: 125px" class="_df">同步错误次数</div>
          </div>

          <div style="margin-top: 4px; display: flex; flex-direction: column; gap: 8px">
            <template v-for="item in data.guild_list">
              <div
                class="_rolebox"
                style="height: 45px; cursor: pointer"
                @click="set.open(item)"
              >
                <div style="width: 200px" class="_df">
                  {{ item.guild_name }}
                </div>
                <div style="width: 125px" class="_df">
                  {{ item.date_data }}
                </div>
                <div style="width: 125px" class="_df">
                  {{ item.created_at }}
                </div>
                <div style="width: 125px" class="_df">
                  {{ item.mslimit }}
                </div>
              </div>
            </template>
          </div>
        </div>
      </div>
      <div class="noa" v-if="$set.mb == true">
        <n-grid x-gap="8" y-gap="8" :cols="parseInt(cfg.sswd / 170)">
          <template v-for="item in data.guild_list">
            <n-gi span="1" @click="set.open(item)">
              <div class="_work_sid_list">
                <n-grid class="ngrid" y-gap="4" x-gap="4" :cols="2">
                  <n-gi span="2">
                    <n-statistic>
                      <div>{{ item.guild_name }}</div>
                    </n-statistic>
                  </n-gi>

                  <n-gi span="1">
                    <n-statistic label="失败次数">
                      {{ item.mslimit }}
                    </n-statistic>
                  </n-gi>
                  <n-gi span="1">
                    <n-statistic label="会战日期"> {{ item.date_data }} </n-statistic>
                  </n-gi>
                </n-grid>
              </div>
            </n-gi>
          </template>
        </n-grid>
      </div>
    </div>
    <m-page :page="data.page" :count="data.count" @ck="set.page_up_page" />
  </div>
</template>

<script>
import { cfg } from "@/cfg/cfg.js";
export default {
  setup() {
    const set = {
      comp: {
        bind_sea: defineAsyncComponent(() => import(`./gsql_bind_sea.vue`)),
      },
      //删除公会全部数据
      del_guild_all: async () => {
        try {
          const guild = data.ckdata;
          const res = await cfg.axios.get(`${cfg.url}`, {
            params: {
              t: "v2/get",
              t2: "admin/guild",
              t3: "del_guild_all",
              id: guild._id.$oid,
              uid: cfg.user.uid,
              authkey: cfg.user.authkey,
            },
          });
          cfg.alert.success({
            title: "OK！",
            content: `${res.data.msg}`,
          });
          data.win_open = false;
          set.get_guild_list(data.page);
        } catch (error) {
          cfg.alert.error({
            title: "ERROR！",
            content: `${error}`,
          });
        }
      },
      //删除公会赛季缓存数据
      del_season_cachefile: async () => {
        try {
          const guild = data.ckdata;
          const res = await cfg.axios.get(`${cfg.url}`, {
            params: {
              t: "v2/get",
              t2: "admin/guild",
              t3: "del_guild_season_cache",
              id: guild._id.$oid,
              uid: cfg.user.uid,
              authkey: cfg.user.authkey,
            },
          });
          cfg.alert.success({
            title: "OK！",
            content: `${res.data.msg}`,
          });
        } catch (error) {
          cfg.alert.error({
            title: "ERROR！",
            content: `${error}`,
          });
        }
      },
      //删除所有公会绑定
      del_allbind: async () => {
        try {
          const guild = data.ckdata;
          const res = await cfg.axios.get(`${cfg.url}`, {
            params: {
              t: "v2/get",
              t2: "admin/guild",
              t3: "del_guild_all_bind",
              id: guild._id.$oid,
              uid: cfg.user.uid,
              authkey: cfg.user.authkey,
            },
          });
          cfg.alert.success({
            title: "OK！",
            content: `${res.data.msg}`,
          });
        } catch (error) {
          cfg.alert.error({
            title: "ERROR！",
            content: `${error}`,
          });
        }
      },
      //打开编辑窗口
      open: (item) => {
        data.ckdata = item;
        data.win_open = true;
      },
      //绑定查询
      open_bind_sea: () => {
        data.win_open_bind_sea = true;
      },
      get_guild_list: async (page) => {
        let res = await cfg.axios.get(`${cfg.url}`, {
          params: {
            t: "v2/get",
            t2: "admin/guild",
            t3: "guild_list",
            page: page,
            uid: cfg.user?.uid,
            authkey: cfg.user?.authkey,
          },
        });
        if (res.data.code == 0) {
          data.guild_list = res.data.data;
          data.count = res.data.count;
        } else {
          cfg.msg.error(res.data.msg);
        }
      },
      //换页
      page_up_page: (page) => {
        data.page = page;
        set.get_guild_list(page);
      },
    };
    let data = reactive({
      guild_list: [],
      count: 0,
      page: cfg.route.query.page ?? 1,
      test_id: "64e5ff886f63a97f111447d5",
      ckdata: {},
      win_open: false,
      win_open_bind_sea: false,
      test_uid: cfg.user.uid,
      test_authkey: cfg.user.authkey,
    });

    onMounted(async () => {
      set.get_guild_list(data.page);
    });
    return { data, cfg, set };
  },
};
</script>
